package com.yzy.soft.system.controller.page;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.BusinessMapper;
import com.yzy.soft.common.persistence.model.Business;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-01-03
 */
@Controller
@RequestMapping("/businesspage")
public class BusinessPageController extends BaseController{
    private static String PREFIX = "/system/page/business/";
    @Autowired
    private BusinessMapper businessMapper;

//    @GetMapping("")
//    public TableDataDto getAll(@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
//        Wrapper<Business> business=new EntityWrapper<Business>();
//        page=(page-1)*pageSize;
//        pageSize=page+pageSize;
//        List<Business> result=businessMapper.selectPage(new RowBounds(page,pageSize),business);
//        TableDataDto tableDataDto=new TableDataDto();
//        tableDataDto.setCode("00");
//        tableDataDto.setMsg("000");
//        //tableDataDto.setCount(businessMapper.getCount().intValue());
//
//        tableDataDto.setCount(businessMapper.selectCount(business));
//        tableDataDto.setData(result);
//        return tableDataDto;
//    }
//
//    @GetMapping("/by")
//    public TableDataDto getCadtion(@ModelAttribute Business business) {
//        Wrapper<Business> businessWrapper=new EntityWrapper<Business>();
//        businessWrapper.where("versionId={0}",business.getVersionId());
//
//        List<Business> result=businessMapper.selectList(businessWrapper);
//        TableDataDto tableDataDto=new TableDataDto();
//        tableDataDto.setCode("00");
//        tableDataDto.setMsg("000");
//        //tableDataDto.setCount(businessMapper.getCount().intValue());
//
//        tableDataDto.setCount(result.size());
//        tableDataDto.setData(result);
//        return tableDataDto;
//    }
//
//
//    @PostMapping("/add")
//    public Tip add(@ModelAttribute Business business) {
//        business.setCreate(new Date());
//        business.setUpdate(new Date());
//        Integer result=businessMapper.insert(business);
//        return SUCCESS_TIP;
//    }
//    @PostMapping("/update")
//    public Tip status(@ModelAttribute Business business) {
//        Integer result=businessMapper.updateById(business);
//        return SUCCESS_TIP;
//    }
////    @DeleteMapping("")
////    public Tip delete(@ModelAttribute Business business) {
////        Integer result=businessMapper.deleteById(business.getId());
////        return SUCCESS_TIP;
////    }
//    @ApiOperation("根据版本编号删除版本信息")
//    @DeleteMapping("/{id}")
//    public Tip delete(@RequestParam(name="ids[]",required = false) String[] rulesversion,@PathVariable(name="id",required = false) String id) {
//        if(StringUtils.isNotBlank(id)){
//            Integer result=businessMapper.deleteById(id);
//        }else if(rulesversion.length!=0){
//            Integer result=businessMapper.deleteBatchIds(Lists.newArrayList(rulesversion) );
//        }
//        return SUCCESS_TIP;
//    }
//
//    @ApiOperation("批量删除版本信息")
//    @DeleteMapping("/")
//    public Tip delete1(@RequestParam(name="ids[]",required = false) String[] rulesversion) {
//        if(rulesversion.length!=0){
//            Integer result=businessMapper.deleteBatchIds(Lists.newArrayList(rulesversion) );
//        }
//        return SUCCESS_TIP;
//    }

    @PutMapping("")
    public Tip update(@ModelAttribute Business business) {
        //System.out.println(business.toString());
        business.setUpdate(new Date());
        //System.out.println(business.toString());
        Integer result=businessMapper.updateById(business);
        return SUCCESS_TIP;
    }
    @ApiOperation("批量版本信息设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) String[] business) {
        if(business.length!=0){
            Integer result=businessMapper.batchNuUseIds(Lists.newArrayList(business) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将版本信息设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) String[] business) {
        if(business.length!=0){
            Integer result=businessMapper.batchUseIds(Lists.newArrayList(business) );
        }
        return SUCCESS_TIP;
    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")String id) {
        Integer result=businessMapper.status(id);
        if(null==result){
            return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }
    @GetMapping("/businesstreeId/{businesstreeId}")
    public TableDataDto getbyAccountId(@PathVariable(name="businesstreeId") String businesstreeId,@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
        Wrapper<Business> expenseattr=new EntityWrapper<>();
        expenseattr.where("businesstreeid={0}",businesstreeId);
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Business> result=businessMapper.selectPage(new RowBounds(page,pageSize),expenseattr);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(businessMapper.selectCount(expenseattr));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @GetMapping("/edit/{businessId}")
    public String editPage(@PathVariable("businessId")String id,Model model) {
        Business business=businessMapper.selectById(id);
        model.addAttribute("business",business);
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
            return PREFIX+"business_edit";
    }
    @GetMapping("/detail/{businessId}")
    public String detailPage(@PathVariable("businessId")String id,Model model) {
        Business business=businessMapper.selectById(id);
        model.addAttribute("business",business);
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
        return PREFIX+"business_detail";
    }

    @GetMapping("/add")
    public String addPage(Model model) {
//        Business business=businessMapper.selectById(id);
//        model.addAttribute("business",business);
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
        return PREFIX+"business_add";
    }


}

